<template>
  <div>
    <KUF2KFMapDialog ref="KUF2KFMapDialog" :crtField="crtField"
                     @setMatchingFieldCallBack="setMatchingFieldCallBack"/>
    <InputDialog ref="setDynamicFieldDialog"
                 :value="crtField.name"
                 label="字段名称"
                 title="设置动态字段名称" @callback="setDynamicFieldCallBack"/>
    <CustomTag2KFDialog ref="CustomTag2KFDialog" :crtField="crtField"
                        @setMatchingFieldCallBack="setMatchingFieldCallBack"/>
    <el-row style="line-height: 35px; ">
      <el-col :span="15">
        <el-button icon="el-icon-back"
                   size="small"
                   type="text"
                   @click="goBack">返回</el-button>
        <span> | 批量新建子任务 </span>
        <el-checkbox style="float: right; " @change="autoSetDynamicField">未匹配字段设置为动态字段</el-checkbox>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="15">
        <el-table ref="knowledgeUnitFieldTable"
                  :data='knowledgeUnitFieldList'
                  :height="tableHeight"
                  stripe border>
          <el-table-column label="素材字段" min-width="150" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-link @click.prevent="matchKnowledgeUnitField(scope.row)">
                {{ scope.row.name }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="count" min-width="50" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="操作" min-width="200">
            <template slot-scope="scope">
              <span><el-link type="primary" @click.prevent="setMatchingField(scope.row)">匹配字段</el-link></span>
              <!-- 目前, 知识单元字段名称和知识名称完全匹配的可以解绑, 修改为动态字段 -->
              <span><el-link type="primary" @click.prevent="setDynamicField(scope.row)">设置动态字段</el-link></span>
              <!-- 舍弃, 已匹配字段, 不想要这部分内容 -->
              <span><el-link :disabled="UTIL.isEmpty(scope.row.knowledgeLibFieldNameDefault)" type="danger"
                             @click.prevent="abandonField(scope.row)">舍弃</el-link></span>
            </template>
          </el-table-column>
          <el-table-column label="知识字段" min-width="150" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="UTIL.isNotEmpty(scope.row.knowledgeLibFieldIdDefault)" style="color: green">(固定)</span>
              <span v-else-if="UTIL.isNotEmpty(scope.row.knowledgeLibFieldNameDefault)" style="color: red">(动态)</span>
              <span v-html="scope.row.knowledgeLibFieldNameDefault"/>
            </template>
          </el-table-column>
        </el-table>
      </el-col>

      <el-col :span="8" :offset="1">
        <el-table ref="knowledgeTable"
                  :data='knowledgeList'
                  :height="tableHeight"
                  stripe border>
          <el-table-column label="知识单元名称" min-width="150" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-html="scope.row.name"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="110">
            <template slot-scope="scope">
              <span><el-link @click.prevent="editKnowledge(scope.row)">修改</el-link></span>
              <!-- 删除, 此次批量新增, 不再包括本知识单元 -->
              <span><el-link @click.prevent="removeKnowledge(scope.row)">删除</el-link></span>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-row style="float: right; margin-right: 10px; margin-top: 5px; ">
      <el-button size="small" @click="$router.go(-1)">取 消</el-button>
      <el-button size="small" @click="saveEditingTask">暂 存</el-button>
      <el-button size="small" @click="insertKnowledgeList" type="primary">确 定</el-button>
    </el-row>
  </div>
</template>

<script>
  import {mapState} from 'vuex'
  import KUF2KFMapDialog from './KUF2KFMapDialog'
  import InputDialog from '../common/InputDialog'
  import CustomTag2KFDialog from "./CustomTag2KFDialog";
  export default {
    name: "MultiCreateKnowledge",
    components: {KUF2KFMapDialog, InputDialog, CustomTag2KFDialog},
    data() {
      return {
        tableHeight: CONST.WORKSPACE_HEIGHT - 85,
        crtField: {}
      }
    },
    computed: {
      ...mapState(['knowledgeUnitFieldList', 'knowledgeList', 'knowledgeLibFieldList']),
    },
    methods: {
      goBack() {
        this.$confirm('您确认要退出该页面，退出后已经进行的操作将不保留', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$router.go(-1)
        }, UTIL.emptyFun)
      },
      // 所有知识对应knowledgeUnitField 赋值;
      setKnowledgeUnitFieldByMulti(crtField) {
        this.knowledgeList.map(item => item.knowledgeUnitFieldList.find(innerItem => innerItem.name === crtField.name))
          .filter(UTIL.nonNullObj)
          .filter(item => UTIL.isEmpty(item.knowledgeLibFieldName))
          .forEach(item => {
            item.knowledgeLibFieldIdDefault = crtField.knowledgeLibFieldIdDefault
            item.knowledgeLibFieldNameDefault = crtField.knowledgeLibFieldNameDefault
          })
      },
      matchKnowledgeUnitField(field) {
        this.$store.commit('setKnowledgeUnitField', JSON.parse(JSON.stringify(field)))
        this.$router.push('/sameKnowledgeUnitField')
      },
      setMatchingField(field) {
        this.crtField = field
        // 考虑自由标签;
        if (field.name === CONST.KNOWLEDGE_UNIT_FIELD_CUSTOM_TAG_KEY) {
          if (this.knowledgeLibFieldList.filter(item => item.kind === CONST.FIELD_KIND_MULTI_VALUE_INPUT).length === 0) {
            this.$message.warning("没有多值类型字段, 不能匹配固定字段")
            return
          }
          else {
            this.$refs.CustomTag2KFDialog.visible = true
            return
          }
        }
        this.$refs.KUF2KFMapDialog.visible = true
      },
      setMatchingFieldCallBack(matchingField) {
        this.crtField.knowledgeLibFieldIdDefault = matchingField.id
        this.crtField.knowledgeLibFieldNameDefault = matchingField.name
        // 所有知识对应knowledgeUnitField 赋值;
        this.setKnowledgeUnitFieldByMulti(this.crtField)
        this.$store.commit('forceUpdate')
      },
      setDynamicField(field) {
        this.crtField = field
        this.$refs.setDynamicFieldDialog.visible = true
      },
      setDynamicFieldCallBack(name) {
        //查看名称是否与库字段名称重复;
        for (let knowledgeLibField of this.knowledgeLibFieldList) {
          if (knowledgeLibField.name === name) {
            this.$message.warning('已存在重名固定字段, 请重新设置动态字段名称')
            return
          }
        }
        this.crtField.knowledgeLibFieldIdDefault = ''
        this.crtField.knowledgeLibFieldNameDefault = name
        // 所有知识对应knowledgeUnitField 赋值;
        this.setKnowledgeUnitFieldByMulti(this.crtField)
        this.$store.commit('forceUpdate')
      },
      autoSetDynamicField(val) {
        if (val) {
          this.knowledgeUnitFieldList
            .filter(item => UTIL.isEmpty(item.knowledgeLibFieldNameDefault))
            .forEach(item => {
              item.knowledgeLibFieldNameDefault = item.name
              item.autoSetDynamicFieldFlag = true
              this.setKnowledgeUnitFieldByMulti(item)
            })
        } else {
          this.knowledgeUnitFieldList
            .filter(item => item.autoSetDynamicFieldFlag)
            .forEach(item => {
              item.knowledgeLibFieldNameDefault = ''
              delete item.autoSetDynamicFieldFlag
              this.setKnowledgeUnitFieldByMulti(item)
            })
        }
        this.$store.commit('forceUpdate')
      },
      abandonField(field) {
        this.$confirm('忽略知识单元相关内容？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          field.knowledgeLibFieldIdDefault = ''
          field.knowledgeLibFieldNameDefault = ''
          // 所有知识对应knowledgeUnitField 赋值;
          this.setKnowledgeUnitFieldByMulti(field)
        }, UTIL.emptyFun)
      },
      editKnowledge(knowledge) {
        this.axios.post('/pmph/action/knowledgeUnit/selectKnowledgeFieldByUnitFieldList', knowledge)
          .then((response) => {
            this.$store.commit('setEditingKnowledge', response.data)
            // store knowledgeList, 替换;
            this.knowledgeList[this.knowledgeList.indexOf(knowledge)] = response.data
            this.$router.push('/createKnowledgeByKnowledgeUnit')
          })
      },
      removeKnowledge(knowledge) {
        this.$confirm('忽略本知识单元？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => this.removeKnowledgeCallback(knowledge), UTIL.emptyFun)
      },
      removeKnowledgeCallback(knowledge) {
        this.knowledgeList.remove(knowledge)
        for (let name of knowledge.knowledgeUnitFieldList.map(item => item.name)) {
          let knowledgeUnitField = this.knowledgeUnitFieldList.find(item => item.name === name)
          knowledgeUnitField.count = knowledgeUnitField.count -1
          if (knowledgeUnitField.count === 0) {
            this.knowledgeUnitFieldList.remove(knowledgeUnitField)
          }
        }
      },
      insertKnowledgeList() {
        this.axios.post('/pmph/action/knowledgeUnit/insertKnowledgeList',
          {knowledgeList: this.knowledgeList,
            knowledgeLibFieldList: this.knowledgeLibFieldList,
            taskItem: this.$store.state.taskItem}).then(() => {
            this.$router.go(-1)
        })
      },
      // 暂存;
      saveEditingTask() {
        this.axios.post('/pmph/action/knowledgeUnit/saveEditingTask',
          {knowledgeList: this.knowledgeList,
            knowledgeLibFieldList: this.knowledgeLibFieldList,
            taskItem: this.$store.state.taskItem}).then(() => {
            this.$router.go(-1)
        })
      },
    },
  }
</script>

<style scoped>

</style>
